<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/4/15
 * Time: 15:08
 */
namespace Admin\Controller;

use Bbs\Api\ForumAttachmentApi;
use Bbs\Api\ForumUserAdmingroupApi;
use Bbs\Api\ForumForumApi;
use Bbs\Api\ForumPostApi;
use Think\Controller;

class BbsForumPostController extends AdminController{

    protected function _initialize() {
        parent::_initialize();
        $this->check(UID);
    }
    //用户权限检查
    private function check($uid){
        $map = array(
            'uid' => $uid,
        );
        $result = apiCall(ForumUserAdmingroupApi::GET_INFO,array($map));
        // dump($result);exit;
        if($result['status']){
            if(empty($result['info']) || (!in_array(intval($result['info']['adminid']), array(1,2)))){
                $this -> error("该用户非管理组");
            }
        }else{
            $this -> error($result['info']);
        }
    }

    public function index(){
        $status = I('status',1);           //1审核 0正常 -1删除
        $this -> assign('status',$status);
        $tid    = I('tid','','int');
        $map = array();
        if(intval($tid) && is_numeric($tid)){
            $this -> assign('tid',$tid);
            $map['tid'] = intval($tid);
        }
        if(!in_array($status,array(-2,-1,0,1))) $this->error('参数错误');
        if($status != -2) $map['status'] = $status;
        $page   = array('curpage' =>  I('get.p',0), 'size' => 15);
        $order  = 'dateline desc';
        $params = $map;
        $result = apiCall(ForumPostApi::QUERY_ALL_POST2,array($map,$page,$order,$params));
        if($result['status']){
            $this->assign('list',$result['info']['list']);
            $this->assign('show',$result['info']['show']);
        }else{
            $this->error($result['info']);
        }
        $this->display();
    }

    /**
     * 帖子详情
     */
    public function detail(){
        $pid = I('get.pid',-1);
        if($pid==-1){
            $this->error('参数错误');
        }
        $map = array(
            'pid' => $pid
        );
        $result = apiCall(ForumPostApi::GET_INFO,array($map));
        if($result['status']){

            $post = $result['info'];
            if(!is_null($post)){
                $post['message'] = htmlspecialchars_decode($post['message']);
            }
            //查询板块
            $result = apiCall(ForumForumApi::GET_INFO,array(array('fid'=>$post['fid'])));
            if($result['status']){
                $forum = $result['info'];
                $this->assign('forum',$forum);
            }
            //查询主题帖
            $result = apiCall(ForumPostApi::GET_INFO,array(array('tid'=>$post['tid'])));
            if($result['status']){
                $thread = $result['info'];
                $this->assign('thread',$thread);
            }
            //查询附件
            $result = apiCall(ForumAttachmentApi::QUERY_NO_PAGING,array(array('pid'=>$pid)));
            if($result['status']){
                $attachment = $result['info'];
                $this->assign('attachment',$attachment);
            }
            $this->assign('post',$post);

        }else{
            $this->error('未知错误');
        }

        $this->display();

    }

    //单个-1删除(默认)·0审核·1重审
    public function delete(){
        $pid    = I('pid',0,'int');
        $status = I('get.status',-1);
        if(!in_array($status,array(-1,0,1))) $this -> error("参数错误");
        $result = apiCall(ForumPostApi::SAVE,array(array('pid'=>$pid),array('status'=>$status)));
        if($result['status']){
            $this -> success("操作成功!");
        }else{
            $this -> error($result['info']);
        }

    }
    // /**
    //  * 通过审核
    //  */
    // public function pass(){

    //    if(IS_AJAX){
    //        $pid = I('get.pid',-1);
    //        if($pid == -1){
    //            $this->error('参数错误');
    //        }

    //        $map = array(
    //            'pid' => $pid
    //        );
    //        $entity = array('invisible' => 1);
    //        $result = apiCall(ForumPostApi::SAVE,array($map ,$entity));
    //        if($result['status']){
    //            $this->success('操作成功!');
    //        }else{
    //            $this->error('操作失败!');
    //        }
    //    }
    // }


    // /**
    //  * 删除帖子
    //  */
    // public function deletePost(){

    //     $map = array('pid' => I('pid', -1));
    //     $result = apiCall(ForumPostApi::DELETE,array($map));

    //     if($result['status']){
    //         $this -> success(L('RESULT_SUCCESS'));
    //     }else{
    //         LogRecord('[INFO]' . $result['info'], '[FILE] ' . __FILE__ . ' [LINE] ' . __LINE__);
    //         $this->error($result['info']);
    //     }

    // }

    /**
     * 批量 -1删除(默认)·0审核·1重审
     */
    public function bulkDeletePost(){
        $pids   = I('ids','');
        $status = I('get.status',-1);
        if($pids == '' || count($pids) == 0){
            $this -> success('操作成功');
        }
        $status = I('get.status',-1);
        if(!in_array($status,array(-1,0,1))) $this -> error("参数错误");

        $result = apiCall(ForumPostApi::SAVE,array(array('pid'=>array('in',$pids)),array('status'=>$status)));
        if($result['status']){
            $this -> success("操作成功");
        }else{
            $this -> error($result['info']);
        }
    }

}